package com.winning.sx.microframework.controller.sysmanage;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.plugins.wx.WxConfig;
import com.winning.sx.microframework.service.EasyService;
import com.winning.sx.microframework.service.base_entity.BaseConstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/servermanage")
public class ServerController {
    @Autowired
    BaseConstService constService;

    @Autowired
    WxConfig wxConfig;
    @Autowired
    EasyService easyService;
    private ObjectMapper objectMapper = new ObjectMapper();
    @RequestMapping(value = {"/", ""})
    public ModelAndView index() throws IOException, CustomException {
        ModelAndView mv = new ModelAndView("server_interface/server");
        List state= constService.queryDropDownList("199");
        mv.addObject("state",state);
   return mv;
    }
    @ResponseBody
    @RequestMapping(value = {"/queryAll"})
    public AjaxTableResponseMessage query(Map<String,String> map) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        try {
            DataTable table = easyService.queryList("SYS_ServerInfo",map);

            ajaxTableResponseMessage.setDatas(table.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) table.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg(e.getMessage());
        }
        return ajaxTableResponseMessage;
    }
    @ResponseBody
    @RequestMapping("/save")
    public AjaxDataResponseMessage saveAsso(String datas) throws CustomException {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.save("SYS_ServerInfo","id",map);

                if(wxConfig.initServer()&&wxConfig.initServerConfig()&&wxConfig.initApiConfig()){
                    ajaxDataResponseMessage.setSuccess(true);
                }else{
                    ajaxDataResponseMessage.setSuccess(false);
                }

            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("保存失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }
    @ResponseBody
    @RequestMapping("/delete")
    public AjaxDataResponseMessage delete(String datas) throws CustomException {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.delete("SYS_ServerInfo","id",map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("保存失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @RequestMapping(value = {"/Server_Config/{id}"})
    public ModelAndView enterprise(@PathVariable("id") String id) throws Exception {
        ModelAndView mv = new ModelAndView("server_interface/server_config");
        mv.addObject("serverId", id);
        List state= constService.queryDropDownList("199");
        mv.addObject("state",state);
        return mv;
    }
    @RequestMapping(value = {"/interfaceM/{id}"})
    public ModelAndView interfaceM(@PathVariable("id") String id) throws Exception {
        ModelAndView mv = new ModelAndView("server_interface/interfaceM");
        mv.addObject("serverId", id);
        List state= constService.queryDropDownList("199");
        mv.addObject("state",state);
        List currentPattern= constService.queryDropDownList("501");
        mv.addObject("currentPattern",currentPattern);
        List logWay= constService.queryDropDownList("111");
        mv.addObject("logWay",logWay);
        List unit= constService.queryDropDownList("503");
        mv.addObject("unit",unit);
        return mv;
    }
}
